import Mock, { Random } from 'mockjs'
import { setPageData, saveData, delData } from '../common-mock-method'

Random.extend({
    roleKey: function () {
        return 'ROLE_' + Mock.mock(/[A-Z]{3,6}/) // Number()
    }
})

let pageData = Mock.mock({
    'list|9': [
        {
            'id|+1': 2,
            createTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
            updateTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
            roleName: '@cname()',
            roleKey: '@roleKey',
            remarks: '@cword(20,30)',
        }
    ]
})
let listData = Mock.mock({
    'list|9': [
        {
            'id|+1': 2,
            createTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
            updateTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
            roleName: '@cname()',
            roleKey: '@roleKey',
            remarks: '@cword(20,30)',
        }
    ]
})
const rolePermits = {
    0: {
        list: [
            {
                id: 3,
                createTime: '2023-11-20 09:59:07',
                updateTime: '2023-11-20 09:59:07',
                permissionName: '系统管理',
                permissionType: 'MENU',
                parentId: 0,
                sort: 2,
                permissionKey: null,
                permissionPath: '/system',
                permissionTopPath: null,
                icon: null,
                children: null
            },
            {
                id: 5,
                createTime: '2023-11-20 10:00:58',
                updateTime: '2023-11-20 10:00:58',
                permissionName: '用户管理',
                permissionType: 'MENU',
                parentId: 3,
                sort: 1,
                permissionKey: null,
                permissionPath: '/user',
                permissionTopPath: '/system',
                icon: 'User',
                children: null
            },
            {
                id: 4,
                createTime: '2023-11-20 10:00:34',
                updateTime: '2023-11-20 10:00:34',
                permissionName: '角色管理',
                permissionType: 'MENU',
                parentId: 3,
                sort: 2,
                permissionKey: null,
                permissionPath: '/role',
                permissionTopPath: '/system',
                icon: 'Key',
                children: null
            },
            {
                d: 6,
                reateTime: '2023-11-20 10:01:42',
                pdateTime: '2023-11-20 10:01:42',
                ermissionName: '菜单与权限',
                ermissionType: 'MENU',
                arentId: 3,
                ort: 3,
                ermissionKey: null,
                ermissionPath: '/menuPermit',
                ermissionTopPath: '/system',
                con: null,
                hildren: null
            },
            {
                id: 7,
                createTime: '2023-11-20 10:02:42',
                updateTime: '2023-11-20 10:02:42',
                permissionName: '系统字典',
                permissionType: 'MENU',
                parentId: 3,
                sort: 4,
                permissionKey: null,
                permissionPath: '/params',
                permissionTopPath: '/system',
                icon: 'Collection',
                children: null
            },
            {
                id: 8,
                createTime: '2023-11-20 10:08:07',
                updateTime: '2023-11-20 10:08:07',
                permissionName: '编辑按钮',
                permissionType: 'BUTTON',
                parentId: 7,
                sort: null,
                permissionKey: 'sys:params:edit',
                permissionPath: null,
                permissionTopPath: null,
                icon: null,
                children: null
            },
            {
                id: 9,
                createTime: '2023-11-20 10:08:17',
                updateTime: '2023-11-20 10:08:17',
                permissionName: '删除按钮',
                permissionType: 'BUTTON',
                parentId: 7,
                sort: null,
                permissionKey: 'sys:params:del',
                permissionPath: null,
                permissionTopPath: null,
                icon: null,
                children: null
            },
            {
                id: 2,
                createTime: '2023-11-20 09:26:15',
                updateTime: '2023-11-20 09:26:15',
                newRecord: false,
                permissionName: '用户添加',
                permissionType: 'BUTTON',
                parentId: 1,
                sort: null,
                permissionKey: 'user:list:add',
                permissionPath: null,
                permissionTopPath: null,
                icon: null,
                children: null
            },
            {
                id: 17,
                createTime: '2023-11-20 15:28:39',
                updateTime: '2023-11-20 15:28:39',
                newRecord: false,
                permissionName: '广场',
                permissionType: 'MENU',
                parentId: 0,
                sort: 100,
                permissionKey: null,
                permissionPath: '/playground',
                permissionTopPath: null,
                icon: null,
                children: null
            },
            {
                id: 18,
                createTime: '2023-11-20 15:29:43',
                updateTime: '2023-11-20 15:29:43',
                permissionName: '测试模块',
                permissionType: 'MENU',
                parentId: 17,
                sort: 1,
                permissionKey: null,
                permissionPath: '/testView',
                permissionTopPath: '/playground',
                children: null
            },
            {
                id: 19,
                createTime: '2023-11-20 15:30:13',
                updateTime: '2023-11-20 15:30:13',
                permissionName: '测试表单',
                permissionType: 'MENU',
                parentId: 18,
                sort: 3,
                permissionKey: null,
                permissionPath: '/testView/formTest',
                permissionTopPath: '/playground',
                icon: 'isForm',
                children: null
            },
            {
                id: 6,
                createTime: '2023-11-20 10:01:42',
                updateTime: '2023-11-20 10:01:42',
                permissionName: '菜单与权限',
                permissionType: 'MENU',
                parentId: 3,
                sort: 3,
                permissionKey: null,
                permissionPath: '/menuPermit',
                permissionTopPath: '/system',
                icon: null,
                children: null
            }
        ]
    },
    1: {
        list: [{
            id: 17,
            createTime: '2023-11-20 15:28:39',
            updateTime: '2023-11-20 15:28:39',
            newRecord: false,
            permissionName: '广场',
            permissionType: 'MENU',
            parentId: 0,
            sort: 100,
            permissionKey: null,
            permissionPath: '/playground',
            permissionTopPath: null,
            icon: null,
            children: null
        },
            {
                id: 18,
                createTime: '2023-11-20 15:29:43',
                updateTime: '2023-11-20 15:29:43',
                permissionName: '测试模块',
                permissionType: 'MENU',
                parentId: 17,
                sort: 1,
                permissionKey: null,
                permissionPath: '/testView',
                permissionTopPath: '/playground',
                children: null
            },
            {
                id: 19,
                createTime: '2023-11-20 15:30:13',
                updateTime: '2023-11-20 15:30:13',
                permissionName: '测试表单',
                permissionType: 'MENU',
                parentId: 18,
                sort: 3,
                permissionKey: null,
                permissionPath: '/testView/formTest',
                permissionTopPath: '/playground',
                icon: 'isForm',
                children: null
            }]
    }
}
module.exports = [
    {
        url: '/mock/sys/role/page',
        method: 'post',
        response: (config) => {
            const pageList = {
                total: 9,
                list: [
                    {
                        id: 1,
                        createTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
                        updateTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
                        roleName: '超级管理员',
                        roleKey: 'ROLE_ADMIN',
                        remarks: '@cword(7,20)'
                    }, ...pageData.list]
            }
            return setPageData(pageList, config, 'roleName', 'roleKey')
        }
    },
    {
        url: '/mock/sys/role/list',
        method: 'post',
        response: () => {
            listData = [
                {
                    id: 1,
                    createTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
                    updateTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
                    roleName: '超级管理员',
                    roleKey: 'ROLE_ADMIN',
                    remarks: '@cword(7,20)'
                }, ...pageData.list]
            return {
                code: 200,
                data: listData,
                message: 'OK'
            }
        }
    },
    { // 根据角色 获取 权限数据
        url: '/mock/sys/role/permission/list',
        method: 'post',
        response: (config) => {
            let result = rolePermits[1].list
            if (config.body.roleIds?.includes(1)) {
                result = rolePermits[0].list
            }
            return {
                code: 200,
                data: result,
                message: 'OK'
            }
        }
    },
    {
        url: '/mock/sys/role/save',
        method: 'post',
        response: (config) => {
            return saveData(config, pageData)
        }
    },
    {   // 删除
        url: RegExp('/mock/sys/role/del/' + '.*'),
        method: 'post',
        response: (config) => {
            return delData(config, '/mock/sys/role/del/', pageData, [1])
        }
    }
]
